<template>
    <div id="app">

        <div id="home">
            <Login v-if="!isLogin"></Login>
            <Admin v-if="isLogin"></Admin>
        </div>

    </div>
</template>

<script>
    import Login from "@/components/login/Login"
    import Admin from "@/components/admin/Admin"
    import {getCookie} from "@/tools/cookie";

    export default {
        name: "",
        components: {
            Login, Admin
        },
        data() {
            return {
                //是否登录，false未登录
                isLogin: false
            }
        },
        methods: {
            login: function (data) {
                this.isLogin = data;
            },
            init: function () {
                let cookieToken = getCookie("token");
                let cookieUserInfo = getCookie("userInfo");
                let cookieIsLogin = getCookie("isLogin");
                if (cookieToken != null) {
                    window.sessionStorage.setItem("token", cookieToken);
                }
                if (cookieUserInfo != null) {
                    window.sessionStorage.setItem("userInfo", cookieUserInfo);
                }
                if (cookieIsLogin != null) {
                    window.sessionStorage.setItem("isLogin", cookieIsLogin);
                }

                let token = window.sessionStorage.getItem("token");
                let userInfo = window.sessionStorage.getItem("userInfo");
                let isLogin = window.sessionStorage.getItem("isLogin");
                let user = JSON.parse(userInfo);
                this.isLogin = isLogin

                this.$store.dispatch("updateId", user == null ? "" : user.userId);
                this.$store.dispatch("updateIsLogin", isLogin);
                this.$store.dispatch("updateToken", token);
                this.$store.dispatch("updateUserInfo", user);
            }
        },
        computed: {
            userInfo: function () {
                return this.$store.getters.getUserInfo
            }
        },
        mounted() {
            console.log("有任何问题,可联系微信: 152 5192 2665");
            this.Bus.$on("my-event-app-login", (data) => {
                this.login(data)
            });
            window.addEventListener("unload", this.init())
        }
    }
</script>

<style>
    * {
        margin: 0px;
        padding: 0px;
        text-decoration: none; /*下划线*/
        list-style: none; /* ul li 的小圆点*/
        font-family: Arial; /*字体*/
        font-size: 16px; /*文字大小*/
    }

    .home {
        width: 100%;
        height: 100%;
        margin: 0px auto;
    }
</style>
